package com.ssdp.config;
import com.ssdp.interceptor.LoginInterceptor;
import com.ssdp.interceptor.RefreshTokenInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author syo 2319615164@qq.com
 * @description 拦截器
 * @date 2024/3/14
 * @Copyright syo
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 路径配置
     */
    private final ExcludeConfig excludeConfig;

    /**
     * 登录拦截器
     */
    private final LoginInterceptor loginInterceptor;

    /**
     * 登录状态刷新拦截器
     */
    private final RefreshTokenInterceptor refreshTokenInterceptor;

    /**
     * 配置拦截器
     * @param registry 注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (excludeConfig.getOpen()) {
            registry.addInterceptor(loginInterceptor)
                    .excludePathPatterns(excludeConfig.getPaths()).order(1);
        }
        registry.addInterceptor(refreshTokenInterceptor).addPathPatterns("/**").order(0);
    }
}
